package com.woniuxy.woniuhis.service.impl;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.Bed;
import com.woniuxy.woniuhis.exception.BedServiceException;
import com.woniuxy.woniuhis.mapper.BedMapper;
import com.woniuxy.woniuhis.service.BedService;
import com.woniuxy.woniuhis.util.UUIDUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class BedServiceImpl implements BedService {
    @Autowired
    private BedMapper bedMapper;

    @Override
    public void addBed(Bed bed) {
        try {
            if (bed.getBedId() == null) {
                bed.setBedId(UUIDUtil.uuid32());
            }
            bedMapper.insertBed(bed);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BedServiceException("服务维护中");
        }
    }

    @Override
    @Transactional
    public void delBedById(String bedId) {
        try {
            if (!StringUtils.isEmpty(bedId)) {
                bedMapper.deleteBedById(bedId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BedServiceException("服务维护中");
        }
    }

    @Override
    public void modifyBed(Bed bed) {
        try {
            if (bed != null) {
                bedMapper.updateBed(bed);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BedServiceException("服务维护中");
        }
    }

    @Override
    public Bed selectBedById(String bedId) {
        try {
            if (!StringUtils.isEmpty(bedId)) {
                return bedMapper.selectBedById(bedId);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BedServiceException("服务维护中");
        }
    }

    @Override
    public PageInfo<Bed> findBedListByCondition(Bed condition, Integer pageNum, Integer pageSize) {
        try {
            if (pageNum == null) {
                pageNum = 0;
            }
            if (pageSize == null || pageSize <= 0) {
                //默认返回五条数据
                pageSize = 5;
            }
            return new PageInfo<Bed>(bedMapper.selectBedListByCondition(condition, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BedServiceException("服务维护中");
        }
    }
}
